﻿Imports DevExpress.XtraRichEdit.API.Native


Public Class UcnRTFeditor
    'represents Rich Text editor

#Region "Attributes"
#Region "Private attributes"
    Private MsOriginalRTFText As String = "" 'original RTF text - should always be in RTF format
    Private MsNewRTFText As String = "" 'new changed RTF text - should always be in RTF format
    Private MsOriginalPlainText As String = "" 'original plain text
    Private MsNewPlainText As String = "" 'new changed plain text
#End Region
#End Region
#Region "Properties"
#Region "Public properties"
    'RTF text - result of this "control"
    Public ReadOnly Property ResultRTFText() As String
        Get
            Return MsNewRTFText
        End Get
    End Property
    'plain text - result of this "control"
    Public ReadOnly Property ResultPlainText() As String
        Get
            Return MsNewPlainText
        End Get
    End Property
    'actual RTF text - result of this "control"
    Public ReadOnly Property ResultActualRTFText() As String
        Get
            Return recRichEditControl.RtfText
        End Get
    End Property
    'actual plain text - result of this "control"
    Public ReadOnly Property ResultActualPlainText() As String
        Get
            Return recRichEditControl.Text
        End Get
    End Property
#End Region
#End Region
#Region "Events"
    Public Event MMValueChangesConfirmed(ByVal NewValue As String) 'event is raised after value of text is confirmed
    Public Event MMValueChangesDiscarded(ByVal NewValue As String) 'event is raised after value of text is discarded
    Public Event MMTextChanging() 'event is raised when text is changing
#End Region
#Region "Events handler"
    'event to notify that text is changing
    Private Sub recRichEditControl_ContentChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles recRichEditControl.ContentChanged
        RaiseEvent MMTextChanging()
    End Sub
    'setting default setting after new document is created
    Private Sub recRichEditControl_EmptyDocumentCreated(ByVal sender As Object, ByVal e As System.EventArgs) Handles recRichEditControl.EmptyDocumentCreated
        Debug.Print("recRichEditControl_EmptyDocumentCreated")

        Dim charProperties As CharacterProperties = recRichEditControl.Document.BeginUpdateCharacters(recRichEditControl.Document.Range)
        charProperties.FontName = "Calibri"
        charProperties.FontSize = 12
        recRichEditControl.Document.EndUpdateCharacters(charProperties)
    End Sub
    'confirmation changes
    Private Sub barBtnConfirmChanges_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnConfirmChanges.ItemClick
        MsNewRTFText = recRichEditControl.RtfText
        MsOriginalRTFText = recRichEditControl.RtfText
        MsNewPlainText = recRichEditControl.Text
        MsOriginalPlainText = recRichEditControl.Text

        RaiseEvent MMValueChangesConfirmed(MsNewRTFText)
    End Sub
    'discarding changes
    Private Sub barBtnDiscardChanges_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles barBtnDiscardChanges.ItemClick
        Try
            recRichEditControl.RtfText = MsOriginalRTFText
        Catch ex As Exception
            recRichEditControl.Text = MsOriginalRTFText
        End Try

        RaiseEvent MMValueChangesDiscarded(MsOriginalRTFText)
    End Sub
#End Region
#Region "Subs"
    'initialization of controls
    Public Sub Init(ByVal IsRtfText As String)
        'creating a new document
        recRichEditControl.CreateNewDocument()

        'filling with text
        Try
            recRichEditControl.RtfText = IsRtfText
        Catch ex As Exception
            recRichEditControl.Text = IsRtfText
        End Try
        MsOriginalRTFText = recRichEditControl.RtfText
        MsNewRTFText = recRichEditControl.RtfText
    End Sub
    'clear text
    Public Sub Clear()
        Init("")
    End Sub
#End Region

End Class

 



   